library(foreign)
library(stargazer)

source("cces_recodes.R")

# allows stargazer to produce output for clmm models 
# (see https://github.com/andrewheiss/civil-society-authoritarian-survival)

fake.clm <- function(x) {
  model.fake <- x
  model.fake$nobs <- x$dims$nobs
  model.fake$call[1] <- call("clm")
  model.fake
}

df.m$fb_d <- recode_01(df.m[,"pctfb15"] - df.m[,"pctfb00"])
df.m$fb_d.n <- df.m[,"pctfb15"] - df.m[,"pctfb00"]
df.m$fb_lag <- recode_01(df.m[,"LAG_P15"])
df.m$fb_lag.n <- df.m[,"LAG_P15"]
df.m$fb_lag_diff <- df.m[,"LAG_P15"] - df.m[,"PCTFB00LAG"]

fit1 <- lmer(imm.scale ~ fb_lag_diff*fb_d + gender + age + educ + faminc + pid7 + ideology + gender + own + hisp + asian + lived + pcthisp_01 + pctasian_01 + pctunemp_01 + medianhsvl_01 + (1|lookupzip), data = df.m, subset = imm == 1)

fit2 <- glmer(trump ~ fb_lag_diff*fb_d + gender + age + educ + faminc + pid7 + ideology + gender + own + hisp + asian + lived + pcthisp_01 + pctasian_01 + pctunemp_01 + medianhsvl_01 + (1|lookupzip), data = df.m, subset = imm == 1, family = binomial(link="logit"), nAGQ = 0)

stargazer(fit1, fit2, covariate.labels = c("Delta Surrounding", "Delta Immigrant", "Geender", "Age", "Education", "Income", "Partisanship", "Ideology", "Home Owner", "Hispanic", "Asian", "Tenure", "Pct. Hispanic", "Pct. Asian", "Pct. Unemployed", "Median Housing Value", "Delta Immigrant x Delta Surrounding", "Intercept"), no.space = TRUE, single.row = TRUE, digits = 2, star.cutoffs = c(.10, .02, .002))

